package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.sky.constant.StatusConstant;
import com.sky.context.ThreadLocalUtil;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Employee;
import com.sky.exception.BusinessException;
import com.sky.mapper.EmployeeMapper;
import com.sky.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 后台登录
     * @param employeeDTO
     * @return
     */
    @Override
    public Employee login(EmployeeDTO employeeDTO) {
        //1. 参数校验-非空
        String username = employeeDTO.getUsername();
        String password = employeeDTO.getPassword();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(password)){
            // 100xx 用户模块异常  200xx 菜品模块异常
            throw new BusinessException(10010,"账号或密码不能为空");
        }
        //2. 业务校验
        //2.1 校验账号是否存在
        Employee emp = employeeMapper.getByUsername(username);
        if(emp == null){
            throw new BusinessException(10011,"账号不存在");
        }
        //2.2 校验密码是否正确
        if(!SecureUtil.md5(password).equals(emp.getPassword())){
            throw new BusinessException(10012,"密码错误");
        }
        //2.3 校验账号状态
        if(emp.getStatus() == StatusConstant.DISABLE){
            throw new BusinessException(10013,"账号已禁用");
        }
        //3. 完成业务
        //4. 返回结果
        return emp;
    }

    /**
     * 新增员工
     * @param employeeDTO
     */
    @Override
    public void add(EmployeeDTO employeeDTO) {
        //参数校验-非空
        //username name phone idNumber
        empParamsCheck(employeeDTO);
        //业务校验
        empBusinessCheck(employeeDTO);
        //完成业务-添加员工(写操作要看表）
        //补全数据-DTO 转PO
        Employee empPO = empDTO2PO(employeeDTO);
        employeeMapper.insert(empPO);
        //返回结果-无
    }
    //补全数据-DTO 转PO
    private Employee empDTO2PO(EmployeeDTO employeeDTO) {
        Employee empPO = BeanUtil.copyProperties(employeeDTO, Employee.class);
        empPO.setPassword(SecureUtil.md5("123456"));
        empPO.setCreateTime(LocalDateTime.now());
        empPO.setUpdateTime(LocalDateTime.now());
        empPO.setCreateUser(ThreadLocalUtil.getCurrentId()); //TODO
        empPO.setUpdateUser(ThreadLocalUtil.getCurrentId()); //TODO
        System.out.println("[Service]当前线程名称："+Thread.currentThread().getName());
        return empPO;
    }
    //业务校验
    private void empBusinessCheck(EmployeeDTO employeeDTO) {
        Employee employee = employeeMapper.getByUsername(employeeDTO.getUsername());
        if(employee != null){
            throw new BusinessException(400,"账号已被注册");
        }
        employee = employeeMapper.getByPhone(employeeDTO.getPhone());
        if(employee != null){
            throw new BusinessException(400,"手机号已被注册");
        }
        employee = employeeMapper.getByIdNumber(employeeDTO.getIdNumber());
        if(employee != null){
            throw new BusinessException(400,"身份证号已被注册");
        }
    }
    //参数校验
    private void empParamsCheck(EmployeeDTO employeeDTO) {
        String username = employeeDTO.getUsername();
        String name = employeeDTO.getName();
        String phone = employeeDTO.getPhone();
        String idNumber = employeeDTO.getIdNumber();
        if(StrUtil.isBlank(username) || StrUtil.isBlank(name)
                || StrUtil.isBlank(phone) || StrUtil.isBlank(idNumber)){
            throw new BusinessException(400,"表单填写不完整，请确认");
        }
    }
}
